package cn.wolfcode.trip.app.web.controller;


import cn.wolfcode.trip.base.domain.CatalogsAttention;
import cn.wolfcode.trip.base.service.IStrategyCatalogService;
import cn.wolfcode.trip.base.util.JSONResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

@Controller
@RequestMapping("/strategyCatalogs")
public class StrategyCatalogController {
    @Autowired
    private IStrategyCatalogService catalogService;


    @GetMapping("/{strategyId}")
    @ResponseBody
    public Object selectCatalog(@PathVariable Long strategyId){
       return catalogService.selectCatalogForLoop(strategyId);
    }

    //点赞回显
    @GetMapping("/chose/{catalogsId}/{userId}")
    @ResponseBody
    public Object chose(@PathVariable("catalogsId") Long catalogsId,
                        @PathVariable("userId")Long userId ) {
        JSONResult result = new JSONResult();
        try {
            int count = catalogService.selectLikeCountByCommentId(catalogsId);
            CatalogsAttention catalogsAttention = catalogService.selectByCatalogsIdAndUserId(catalogsId, userId);
            if (catalogsAttention == null) {
                result.setSuccess(false);
            } else {
                result.setSuccess(true);
            }
            result.setData(count);
        } catch (Exception e) {
            e.printStackTrace();
            result.mark(e.getMessage());
        }
        return result;
    }

    //取消点赞
    @DeleteMapping("/deleteChose/{catalogsId}/{userId}")
    @ResponseBody
    public Object deleteChose(@PathVariable("catalogsId") Long catalogsId,
                              @PathVariable("userId")Long userId ) {
        JSONResult result = new JSONResult();
        try {
            catalogService.deleteChose(catalogsId, userId);
            result.setSuccess(true);
        } catch (Exception e) {
            e.printStackTrace();
            result.mark("网络异常,操作失败");
        }
        return result;
    }

    //进行点赞
    @PostMapping("/insertChose/{catalogsId}/{userId}")
    @ResponseBody
    public Object insertChose(@PathVariable("catalogsId") Long catalogsId,
                              @PathVariable("userId")Long userId ) {
        JSONResult result = new JSONResult();
        try {
            catalogService.insertChose(catalogsId, userId);
            result.setSuccess(true);
        } catch (Exception e) {
            e.printStackTrace();
            result.mark("网络异常,操作失败");
        }
        return result;
    }
    
}
